import Vue from 'vue'
import Vuex from 'vuex'
import { login, UserId } from '@/api'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: localStorage.getItem('token') || '',
    User: JSON.parse(localStorage.getItem('User')) || {}
  },
  mutations: {
    setToken (state, token) {
      state.token = token
      localStorage.setItem('token', token)
      console.log(token, '12313')
    },
    UserToken (state, UserId) {
      state.User = UserId
      localStorage.setItem('User', JSON.stringify(UserId))
    }
  },
  actions: {
    async logins (store, value) {
      const res = await login(value)
      store.commit('setToken', res.data.token)
      const User = await UserId(res.data.userId)
      store.commit('UserToken', { ...User, ...res.data })
    }
  },
  modules: {
  }
})
